<template>
  <div class="login">
    <div class="login-form">
      <div class="login-header">
        <img src="../../assets/images/logo.png" width="195" height="48" alt="">
      </div>
      <el-input placeholder="请输入用户名" suffix-icon="fa fa-user" v-model="form.username" style="margin-bottom: 18px">
      </el-input>

      <el-input placeholder="请输入密码" suffix-icon="fa fa-keyboard-o" v-model="form.password" type="password" style="margin-bottom: 18px" @keyup.native.enter="login">
      </el-input>

      <el-button type="primary" :loading="loginLoading" style="width: 100%;margin-bottom: 18px" @click.native="login">登录
      </el-button>
      <div>
        <el-checkbox v-model="Remenber"> Remenber</el-checkbox>
        <router-link to="/register" style="float: right;color: #3C8DBC;font-size: 14px">Register</router-link>
      </div>

    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      Remenber: true,
      loginLoading: false
    }
  },
  created() {
    this.isLogin();
  },
  methods: {
    login() {
      this.axios.post('/users/login', {
        ...this.form
      }).then((ret) => {
        const { data } = ret.data;
        this.store.set('userInfo', data)
        this.loginLoading = true;
        setTimeout(() => {
          this.$notify({
            title: '登录成功',
            message: '欢迎你！'+ret.data.data.username,
            type: 'success'
          });
          this.loginLoading = false;
          this.$router.push({ path: '/' });
        }, 1000);
      }).catch(() => {
        this.$message.error('用户名和密码错误');
      })
    },
    isLogin() {
      this.axios.get('/users/islogin').then((ret) => {
        let { data, error } = ret.data;
        if (data == 1) {
          this.$router.push('/')
          return;
        }
      }).catch(() => { })
    },
  }
}
</script>

<style lang="less">
@import "Login.less";
</style>
